package com.mango.ch05;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

public class PairOfWords implements WritableComparable<PairOfWords> {
	private String leftEle = new String();
	private String rightEle = new String();

	public PairOfWords() {

	}

	public PairOfWords(String leftEle, String rightEle) {
		this.leftEle = leftEle;
		this.rightEle = rightEle;
	}

	public void setWord(String word) {
		this.leftEle = word;
	}

	public void setNeighbor(String neighbor) {
		this.rightEle = neighbor;
	}

	public String getLeftEle() {
		return leftEle;
	}

	public String getFirst() {
		return leftEle;
	}

	public void setLeftEle(String leftEle) {
		this.leftEle = leftEle;
	}

	public String getRightEle() {
		return rightEle;
	}

	public String getSecond() {
		return rightEle;
	}

	public void setRightEle(String rightEle) {
		this.rightEle = rightEle;
	}

	@Override
	public String toString() {
		return "PairOfWords [leftEle=" + leftEle + ", rightEle=" + rightEle + "]";
	}

	@Override
	public void write(DataOutput out) throws IOException {
		// TODO Auto-generated method stub
		out.writeUTF(this.leftEle);
		out.writeUTF(this.rightEle);
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		// TODO Auto-generated method stub
		this.leftEle = in.readUTF();
		this.rightEle = in.readUTF();
	}

	@Override
	public int compareTo(PairOfWords pair) {
		String pl = pair.getLeftEle();
		String pr = pair.getRightEle();
		// 排序，有*的排到前面，使其可以先算总数
		if (leftEle.equals(pl)) {
			return rightEle.compareTo(pr);
		}
		return leftEle.compareTo(pl);

	}

}
